package com.firmiana.jt809.util;

/**
 * @author hucl
 * @date 2020/8/25 11:26
 */
public class Tools {
    public final static long MAX = (1L << 32) - 1;


    public static String turnDataLength(String data, int length) {
        int data_length = data.length();
        StringBuilder dataBuilder = new StringBuilder(data);
        for (int i = data_length; i < length; i++) {
            dataBuilder.insert(0, "0");
        }
        data = dataBuilder.toString();
        return data;
    }

    public static String toHexString(long data, int byteNum) {
        int totalLen = byteNum * 2;
        StringBuilder tmp = new StringBuilder(Long.toHexString(data));
        while (tmp.length() < totalLen) {
            tmp.insert(0, "0");
        }
        int start = tmp.length() - totalLen;
        tmp = new StringBuilder(tmp.substring(start));
        return tmp.toString();
    }

    public static String toHexString(byte[] bts) {
        StringBuilder strBuild = new StringBuilder();
        for (byte bt : bts) {
            strBuild.append(toHexString(bt));
        }
        return strBuild.toString();
    }

    //byte字节转16进制字符串
    public static String toHexString(byte data) {
        return Integer.toHexString(data & 0xff | 0x100).substring(1);
    }

    public static String getCRCString(String buff) {
        int crc = 0xFFFF; // initial value
        int polynomial = 0x1021; // 0001 0000 0010 0001 (0, 5, 12)
        byte[] data = hexString2Bytes(buff);
        for (byte b : data) {
            // char b = buf.charAt(j);
            for (int i = 0; i < 8; i++) {
                boolean bit = ((b >> (7 - i) & 1) == 1);
                boolean c15 = ((crc >> 15 & 1) == 1);
                crc <<= 1;
                if (c15 ^ bit)
                    crc ^= polynomial;
            }
        }
        crc &= 0xffff;
        // (crc % 256);
        return toHexString(crc, 2);
    }

    public static byte[] hexString2Bytes(String hexStr) {
        byte[] b = new byte[hexStr.length() / 2];
        int j = 0;

        for (int i = 0; i < b.length; i++) {
            char c0 = hexStr.charAt(j++);
            char c1 = hexStr.charAt(j++);
            b[i] = ((byte) (parse(c0) << 4 | parse(c1)));
            //int start = i * 2;
            //int c = Integer.parseInt(hexstr.substring(start, start + 2));
            //b[i] = (byte)c;
        }
        return b;
    }

    private static int parse(char c) {
        if (c >= 'a') {
            return c - 'a' + 10 & 0xF;
        }
        if (c >= 'A') {
            return c - 'A' + 10 & 0xF;
        }
        return c - '0' & 0xF;
    }

    public static String encoderStringEscape(String strEscape) {
        strEscape = strEscape.toUpperCase();
        int byteNum = strEscape.length() / 2;
        StringBuilder sb = new StringBuilder();
        for (int m = 0; m < byteNum; m++) {
            int start = m * 2;

            String temp = strEscape.substring(start, start + 2);
            if (temp.equals("5A"))
                sb.append("5A02");
            else if (temp.equals("5B"))
                sb.append("5A01");
            else if (temp.equals("5E"))
                sb.append("5E02");
            else if (temp.equals("5D"))
                sb.append("5E01");
            else
                sb.append(temp);
        }
        // strEscape = strEscape.replaceAll("5A", "5A02");
        // strEscape = strEscape.replaceAll("5B", "5A01");
        // strEscape = strEscape.replaceAll("5E", "5E02");
        // strEscape = strEscape.replaceAll("5D", "5E01");
        return sb.toString();
    }
}
